package vn.vietel.pms.business;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;

import org.apache.commons.logging.Log;
import org.jboss.seam.annotations.Logger;
import org.springframework.transaction.annotation.Transactional;

import vn.vietel.pms.entity.VTypeOfProperty;
import vn.vietel.pms.service.TypeOfPropertyService;

public class TypeOfPropertyServiceImpl implements TypeOfPropertyService {

	@Logger
	private Log log;

	@PersistenceContext
	private EntityManager entityManager;

	@Transactional
	public void create(VTypeOfProperty type) {
		if (type != null) {
			entityManager.persist(type);
		}
	}

	@Transactional
	public VTypeOfProperty find(Long id) {
		return entityManager.find(VTypeOfProperty.class, id);
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	@Transactional
	public void delete(VTypeOfProperty type) {
		entityManager.remove(type);

	}

	@Transactional
	public VTypeOfProperty find(String typeName) {
		try {
			return (VTypeOfProperty) entityManager
					.createQuery(
							"select g from VTypeOfProperty g where g.typename=:typename")
					.setParameter("typename", typeName).getSingleResult();
		} catch (PersistenceException e) {
			log.info("Could not found the VGroupOfProperty\n" + e.getMessage());
			return null;
		}
	}

	@Transactional
	public VTypeOfProperty find(String typeName, String typeDesc) {
		try {
			return (VTypeOfProperty) entityManager
					.createQuery(
							"select g from VTypeOfProperty g where g.typename=:typename and g.groupDesc=:groupDesc")
					.setParameter("groupname", typeName).setParameter(
							"groupDesc", typeDesc).getSingleResult();
		} catch (PersistenceException e) {
			log.info("Could not found the VGroupOfProperty\n" + e.getMessage());
			return null;
		}
	}

	@Transactional
	public void update(VTypeOfProperty type) {
		entityManager.merge(type);

	}

	@SuppressWarnings("unchecked")
	public List<VTypeOfProperty> getAll() {

		try {
			javax.persistence.Query qr = entityManager
					.createQuery("select g from VTypeOfProperty g ");
			return qr.getResultList();

		} catch (PersistenceException e) {
			log.info("Could not found the VTypeOfProperty\n" + e.getMessage());
			return null;
		}
	}

}
